#pragma once
//============================================================================
// Copyright (C) 2003-2010 Brett R. Jones
// All Rights Reserved
//
// You may redistribute and/or modify for non commercial and commercial uses
// provided this copyright notice remains in place and is not modified
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// brettjones1900@gmail.com
// http://www.tronacom.com  http://www.slytron.com  http://www.p2panarchy.com
//============================================================================

#define MAX_CONCURRENT_SQL_QUERIES 3
class DbBase
{
public:
	//=== vars ===//
	std::string			m_strDbFileName;
    sqlite3 *			m_poDb;
	BOOL				m_bDbInitialized;		// if true database initialized
	sqlite3_stmt *		m_aiSqlHandles[ MAX_CONCURRENT_SQL_QUERIES ];		// 

	//=== constructor ===//
	DbBase();
	//=== destructor ===//
	~DbBase();

	//! Initialize the database
	virtual RCODE	DbStartup( const char * pDbFileName );
	//! shutdown the database
	virtual RCODE	DbShutdown( void );

	//! open the database
	virtual RCODE	DbOpen( void );
	//! close the database
	virtual RCODE	DbClose( void );


	//! create initial database
	virtual RCODE	DbCreateDatabase( void );
	//! create initial tables in database 
	virtual RCODE	DbCreateTables( void ) = 0;
 
	//! archive database into given directory
	virtual RCODE	DbArchive( const char * pArchiveDirectory, BOOL bClearExistingDb = false );

	//! open db then execute Sql statement then close db
	virtual RCODE	SqlExec( char * pSqlStatment );
	//! open db then execute Sql statement then close db
	virtual RCODE	SqlExec( const char * pSqlString, ... );

	//! execute Sql statement without opening or closing the db
	virtual RCODE	RunQuery( const char * pSqlStatment );

	//! execute sql then return int from first column and close query
	virtual S32		QueryAndReturnFirstColumnInteger( const char * pSqlStatment );


	//! Runs a query for multiple rows of data
	virtual RCODE	StartDataQuery( const char * pSqlString, int iSqlHandle = 0 );
	//! fetch next row from table
	virtual RCODE	GetDbRow( int iSqlHandle = 0 );
	//! close query
	virtual RCODE	CloseQuery( int iSqlHandle = 0 );
	//! return 32 bit integer from column
	virtual S32		GetDbS32( int iColumnIdx, int iSqlHandle = 0 );
	//! return 64 bit integer from column
	virtual S64		GetDbS64( int iColumnIdx, int iSqlHandle = 0 );
	//! return float from column
	virtual F32		GetDbF32( int iColumnIdx, int iSqlHandle = 0 );
	//! return float from column
	virtual F64		GetDbF64( int iColumnIdx, int iSqlHandle = 0 );
	//! return string from column
	virtual const char * GetDbString(int iColumnIdx, int iSqlHandle = 0 );


	//! get the row id of the last inserted row
	virtual S64		GetLastInsertId( void );


};


